package com.test.burnincash.util_burnin.analytics_burnin.util_burnin

import android.app.Application
import com.test.burnincash.activity_fragment_burnin.base_burnin.BaseApplication_burnin
import com.test.burnincash.constant_burnin.Url_burnin
import com.test.burnincash.util_burnin.analytics_burnin.bean_burnin.ParamFacebookS2S_burnin
import com.test.burnincash.util_burnin.analytics_burnin.http_burnin.HttpHelper_burnin
import com.test.burnincash.util_burnin.analytics_burnin.timezone_burnin.TimeZoneUtil_burnin
import com.facebook.FacebookSdk
import com.facebook.appevents.AppEventsLogger
import com.google.gson.Gson
import com.zhi.syc.data.util.ASStoreUtil
import java.math.BigDecimal
import java.math.RoundingMode

object FacebookUtil_burnin {

    /**
     * 初始化facebook模块
     *
     * @param context Application
     */
    fun init(context: Application) {
        try {
            FacebookSdk.sdkInitialize(context)
            AppEventsLogger.activateApp(context)
        } catch (e: java.lang.Exception) {
            e.printStackTrace()
        }
    }

    /**
     * 保存facebook s2s信息
     */
    fun saveS2SInfo(targetUrlList: List<String>) {
        val params = ParamFacebookS2S_burnin()
        val body = Gson().toJson(params).trim()
        val resultTargetUrlList = TimeZoneUtil_burnin.covertToRealTargetServers(targetUrlList)
        println("====== $resultTargetUrlList")

        //传多国时，里触发多次
        resultTargetUrlList.forEach { path ->
            val pathFormat =
                if (path.endsWith(Url_burnin.PATH_FIREBASE_S2S)) path else (path + Url_burnin.PATH_FACEBOOK_S2S)
            HttpHelper_burnin.postData(pathFormat, body)
        }
    }

    /**
     * 获取总存储大小
     */
    fun getTotalRom(): String {
        var totalRom = ASStoreUtil.getCashTotal(BaseApplication_burnin.getApplication_burnin())
        if (totalRom.endsWith("TB")) {
            totalRom = totalRom.replace("TB".toRegex(), "")
            totalRom =
                BigDecimal(totalRom).multiply(BigDecimal(1024)).stripTrailingZeros().toPlainString()
        } else if (totalRom.endsWith("GB")) {
            totalRom = totalRom.replace("GB".toRegex(), "")
        } else if (totalRom.endsWith("MB")) {
            totalRom = totalRom.replace("MB".toRegex(), "")
            totalRom = BigDecimal(totalRom).divide(BigDecimal(1024), RoundingMode.HALF_UP)
                .stripTrailingZeros().toPlainString()
        }
        return totalRom
    }

    /**
     * 获取可用存储大小
     */
    fun getAvalidableRom(): String {
        var totalRom =
            ASStoreUtil.getCashCanUse(BaseApplication_burnin.getApplication_burnin())
        if (totalRom.endsWith("TB")) {
            totalRom = totalRom.replace("TB".toRegex(), "")
            totalRom =
                BigDecimal(totalRom).multiply(BigDecimal(1024)).stripTrailingZeros().toPlainString()
        } else if (totalRom.endsWith("GB")) {
            totalRom = totalRom.replace("GB".toRegex(), "")
        } else if (totalRom.endsWith("MB")) {
            totalRom = totalRom.replace("MB".toRegex(), "")
            totalRom = BigDecimal(totalRom).divide(BigDecimal(1024), RoundingMode.HALF_UP)
                .stripTrailingZeros().toPlainString()
        }
        return totalRom
    }
}